package com.atguigu.gmall.mq.receiver;

import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author atguigu-mqx
 */
@Component
@Configuration
public class ConfirmReceiver {

    //  什么时候直接监听队列?  什么时候需要做绑定? 当队列存在的时候，设置了交换机与队列的绑定关系时！ 反之，需要绑定！
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue.confirm", durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = "exchange.confirm"),
            key = {"routing.confirm"}
    ))
    public void getMsg(String msg, Message message, Channel channel) {
        System.out.println("快跑:" + msg);
        System.out.println("快跑:" + new String(message.getBody()));

        //  确认消息
        //  第二参数表示确认方式，false 表示一个一个消息确认！ true 表示批量确认！
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        //  监听消息！监听队列2
    }
        @SneakyThrows
        @RabbitListener(queues = DeadLetterMqConfig.queue_dead_2)
        public void getMsg1 (String msg, Message message, Channel channel) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("接收时间:" + simpleDateFormat.format(new Date()) + msg);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }

//          监听基于插件的消息！

        @RabbitListener(queues = DelayedMqConfig.queue_delay_1)
        public void getMsg2 (String msg, Message message, Channel channel) throws IOException {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("接收时间:" + simpleDateFormat.format(new Date()) + msg);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }

    }
